package com.platform.config;

import com.platform.common.utils.StringUtils;
import com.platform.modules.phone.config.PhoneCommonConfig;
import com.platform.modules.phone.config.PhoneSelInfoConfig;
import com.platform.modules.phone.config.PhoneTopUpConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author xiaoemo
 * 手机充值 配置文件(初始化 对应配置文件的bean)
 */
@Configuration
public class PhoneTopupConfiguration {

    @Bean("phoneCommonConfig")
    public PhoneCommonConfig phoneCommonConfig(@Autowired Environment env) {
        // 获取对应属性
        String merId = env.getProperty("phone.common.merID");
        String key = env.getProperty("phone.common.key");
        String merAccount = env.getProperty("phone.common.merAccount");

        PhoneCommonConfig config = new PhoneTopUpConfig();
        config.setKey(key);
        config.setMerAccount(merAccount);
        config.setMerID(merId);
        return config;
    }


    @Bean("phoneTopupConfig")
    public PhoneTopUpConfig phoneTopupConfig(@Autowired Environment env,
                                             @Qualifier("phoneCommonConfig") PhoneCommonConfig commonConfig) {
        // 获取充值对应的配置属性
        String requestUrl = env.getProperty("phone.topup.requestUrl");
        String notifyUrl = env.getProperty("phone.topup.notifyUrl");
        String timeoutStr = env.getProperty("phone.topup.timeout");
        Long timeout = StringUtils.isEmpty(timeoutStr) ? null : Long.valueOf(timeoutStr);

        PhoneTopUpConfig config = new PhoneTopUpConfig();
        config.setMerAccount(commonConfig.getMerAccount());
        config.setKey(commonConfig.getKey());
        config.setMerID(commonConfig.getMerID());

        config.setNotifyUrl(notifyUrl);
        config.setTimeout(timeout);
        config.setRequestUrl(requestUrl);
        return config;
    }

    @Bean("phoneSelInfoConfig")
    public PhoneSelInfoConfig phoneSelInfoConfig(@Autowired Environment env,
                                                 @Qualifier("phoneCommonConfig") PhoneCommonConfig commonConfig) {
        // 获取充值对应的配置属性
        String requestUrl = env.getProperty("phone.selInfo.requestUrl");
        String notifyUrl = env.getProperty("phone.selInfo.notifyUrl");

        PhoneSelInfoConfig config = new PhoneSelInfoConfig();
        config.setMerAccount(commonConfig.getMerAccount());
        config.setKey(commonConfig.getKey());
        config.setMerID(commonConfig.getMerID());

        config.setNotifyUrl(notifyUrl);
        config.setRequestUrl(requestUrl);
        return config;
    }


}
